from collections import deque

print('From the right:')
d = deque('abcdefg')
while True:
    try:
        print(d.pop(), end=' ')
    except IndexError:
        break
print()

print('From the left:')
d = deque('abcdefg')
while True:
    try:
        print(d.popleft(), end=' ')
    except IndexError:
        break
print()